#ifndef __MYTIMEFD__
#define __MYTIMEFD__

#include <iostream>
#include <sys/timerfd.h>
#include <cstring>
#include <errno.h>

#include "Log.hpp"

class MyTimefd
{
public:
    static int CreateTimefd()
    {
        int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
        if (timerfd == -1)
        {
            Logmessage(INFO, "create timefd fail");
        }
        return timerfd;
    }

    // 设置时间，同时兼顾刷新的功能
    static int SetTime(int timefd, size_t first_timeout, size_t timeout)
    {
        struct itimerspec its;
        its.it_value.tv_sec = first_timeout; // 定时器首次触发前等待5秒
        its.it_value.tv_nsec = 0;
        its.it_interval.tv_sec = timeout; // 每隔5秒触发一次
        its.it_interval.tv_nsec = 0;

        if (timerfd_settime(timefd, 0, &its, NULL) == -1)
        {
            Logmessage(ERROR, "time set fail %d, %s", timefd, strerror(errno));
        }
    }




};

#endif
